﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Paint
{
    class HinhVe
    {
        public HinhVe()
        {
            this._bimap = new Bitmap(5,5);
            this._graphics = Graphics.FromImage(this._bimap);
            this._graphics.SmoothingMode = SmoothingMode.HighQuality;
            this._diemDau = new Point();
            this._diemCuoi= new Point();
            this._color = Color.Black;
            this._choiVe = new SolidBrush(this._color);
            this._weight = 1;
            this._pen = new Pen(this._choiVe,this._weight);
        }

        protected Bitmap _bimap;

        public Bitmap Bimap
        {
            get { return _bimap; }
            set { _bimap = value; }
        }

        protected Graphics _graphics;
        
        public Graphics Graphics
        {
            get { return _graphics; }
            set { _graphics = value; }
        }

        protected Point _diemDau;

        public Point DiemDau
        {
            get { return _diemDau; }
            set { _diemDau = value; }
        }
        protected Point _diemCuoi;

        public Point DiemCuoi
        {
            get { return _diemCuoi; }
            set { _diemCuoi = value; }
        }
        protected Pen _pen;

        public Pen Pen
        {
            get { return _pen; }
            set { _pen = value; }
        }
        protected Color _color;

        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        protected SolidBrush _choiVe;

        public SolidBrush ChoiVe
        {
            get { return _choiVe; }
            set { _choiVe = value; }
        }
        protected int _weight;        

        public int Weight
        {
            get { return _weight; }
            set { _weight = value; }
        }

        public virtual void VeHinh()
        {

        }

        public void MoHinh(Panel panel,HinhVe hv)
        {
            OpenFileDialog of = new OpenFileDialog() { Filter = "Anh bmp|*.bmp|Anh jpg va png|*.jpg;*.png|Tat ca dinh dang (*.*)|*.*" };

            if (of.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                panel.BackgroundImage = Image.FromFile(of.FileName);
                HienThiHinh(panel,hv);
            }
        }

        public void LuuHinh(Panel panel, HinhVe hv)
        {
            //http://msdn.microsoft.com/en-us/library/aa984409(v=vs.71).aspx
            panel.DrawToBitmap(hv.Bimap, new Rectangle(0, 0, panel.Width, panel.Height));
            if (hv.Bimap != null)
            {
                SaveFileDialog SaveFD = new SaveFileDialog() { FileName = "Image", /*SaveFD.InitialDirectory = "C";*/Title = "Save As", Filter = "Bitmap Image|*.bmp|JPeg Image|*.jpg|Gif Image|*.gif" };
                if (SaveFD.ShowDialog() == DialogResult.OK)
                {
                    switch (SaveFD.FilterIndex)
                    {
                        case 1:
                            hv.Bimap.Save(SaveFD.FileName, ImageFormat.Bmp);
                            break;
                        case 2:
                            hv.Bimap.Save(SaveFD.FileName, ImageFormat.Jpeg);
                            break;
                        case 3:
                            hv.Bimap.Save(SaveFD.FileName, ImageFormat.Gif);
                            break;
                    }
                }
            }
        }

        public void InHinh()
        {
            
        }

        public void HienThiHinh(Panel panel, HinhVe hv)
        {
            panel.DrawToBitmap(hv.Bimap, new Rectangle(0, 0, panel.Width, panel.Height));
            panel.BackgroundImage = hv.Bimap;
        }

        public void ChangeColor(HinhVe hv, Color c)
        {
            hv.Color = c;
            hv.Pen = new Pen(hv.Color, hv.Weight);
        }

        public void ChangeWeight(HinhVe hv, int weight)
        {
            hv.Weight = weight;
            hv.Pen = new Pen(hv.Color, hv.Weight);
        }
    }
}
